package cn.ruc.dbiir.warn.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

import cn.ruc.dbiir.warn.entity.WarnWater;

public class DBUtil {
	
	
	public static Connection getConn() {
	    String driver = "com.mysql.jdbc.Driver";
	    String url = "jdbc:mysql://localhost:3306/monitor";
	    String username = "root";
	    String password = "xiaojian";
	    Connection conn = null;
	    try {
	        Class.forName(driver); //classLoader,加载对应驱动
	        conn = (Connection) DriverManager.getConnection(url, username, password);
	    } catch (ClassNotFoundException e) {
	        e.printStackTrace();
	    } catch (SQLException e) {
	        e.printStackTrace();
	    }
	    return conn;
	}
	
	
	public static int insert(WarnWater warnWater) {
	    Connection conn = getConn();
	    int i = 0;
	    String sql = "insert into warn_water(node_id, node_disk_rate, node_mem_rate, node_cpu_rate, warn_code) values(?,?,?,?,?);";
	    PreparedStatement pstmt;
	    try {
	        pstmt = (PreparedStatement) conn.prepareStatement(sql);
	        pstmt.setInt(1, warnWater.getNode_id());
	        pstmt.setInt(2, warnWater.getNode_disk_rate());
	        pstmt.setInt(3, warnWater.getNode_mem_rate());
	        pstmt.setInt(4, warnWater.getNode_cpu_rate());
	        pstmt.setInt(5, warnWater.getWarn_code());
	        i = pstmt.executeUpdate();
	        pstmt.close();
	        conn.close();
	    } catch (SQLException e) {
	        e.printStackTrace();
	    }
	    return i;
	}
	
	
//	private static int update(WarnWater warnWater) {
//	    Connection conn = getConn();
//	    int i = 0;
//	    String sql = "update students set Age='" + student.getAge() + "' where Name='" + student.getName() + "'";
//	    PreparedStatement pstmt;
//	    try {
//	        pstmt = (PreparedStatement) conn.prepareStatement(sql);
//	        i = pstmt.executeUpdate();
//	        System.out.println("resutl: " + i);
//	        pstmt.close();
//	        conn.close();
//	    } catch (SQLException e) {
//	        e.printStackTrace();
//	    }
//	    return i;
//	}
	
	
	public static String getWarnWater() {
	    Connection conn = getConn();
	    String sql = "select warn_water.node_disk_rate,warn_water.node_mem_rate,warn_water.node_cpu_rate,warn_water.warn_time,"
	    		+ "warn_type.warn_code,warn_type.warn_msg from warn_water,warn_type where warn_water.warn_code = warn_type.warn_code order by warn_time desc limit 1";
	    PreparedStatement pstmt;
	    String reMsg="";
	    try {
	        pstmt = (PreparedStatement)conn.prepareStatement(sql);
	        ResultSet rs = pstmt.executeQuery();
	        int col = rs.getMetaData().getColumnCount();
	        while (rs.next()) {
	           String ndr = rs.getString("node_disk_rate");
	           String nmr = rs.getString("node_mem_rate");
	           String ncr = rs.getString("node_cpu_rate");
	           String wc = rs.getString("warn_code");
	           String wt = rs.getString("warn_time");
	           String wm = rs.getString("warn_msg");
	           reMsg = ndr+","+nmr+","+ncr+","+wc+","+wt+","+wm;
	        }
	        pstmt.close();
	        conn.close();
	    } catch (SQLException e) {
	        e.printStackTrace();
	    }
	    return reMsg;
	}
	
	
	
//	private static int delete(String name) {
//	    Connection conn = getConn();
//	    int i = 0;
//	    String sql = "delete from students where Name='" + name + "'";
//	    PreparedStatement pstmt;
//	    try {
//	        pstmt = (PreparedStatement) conn.prepareStatement(sql);
//	        i = pstmt.executeUpdate();
//	        System.out.println("resutl: " + i);
//	        pstmt.close();
//	        conn.close();
//	    } catch (SQLException e) {
//	        e.printStackTrace();
//	    }
//	    return i;
//	}
	
	
	public static void main(String[] args) {
		
	}
	
}
